package com.easonji.attendance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.attendance.mapper.OvertimeMapper;
import com.easonji.attendance.mapper.OvertimeStateMapper;
import com.easonji.attendance.model.Overtime;
import com.easonji.attendance.model.OvertimeState;
import com.easonji.attendance.service.IOvertimeStateService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.exception.BizException;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 加班状态 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-09-28
 */
@Service
public class OvertimeStateServiceImpl extends AbstractBaseService<OvertimeState> implements IOvertimeStateService {

    @Resource
    private OvertimeStateMapper overtimeStateMapper;

    @Resource
    private OvertimeMapper overtimeMapper;

    @Override
    protected JSONObject warp2Json(OvertimeState overtimeState) {
       JSONObject result = JsonUtils.parseObject(overtimeState);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<OvertimeState> page = new PageFactory<OvertimeState>().defaultPage();
        // 查询条件
        Wrapper<OvertimeState> wrapper = new EntityWrapper<>();

        List<OvertimeState> overtimeStates = overtimeStateMapper.selectPage(page, wrapper);

        return new PageInfo(warp(overtimeStates), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer otsId) {
        OvertimeState overtimeState = overtimeStateMapper.selectById(otsId);
        return warp(overtimeState);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = BizException.class)
    public Integer save(OvertimeState overtimeState) {

        ShiroUser shiroUser = ShiroUtils.getUser();

        overtimeState.setOtsDatetime(new Date());

        OvertimeState overtimeState_old = new OvertimeState();
        BeanUtils.copyProperties(overtimeState,overtimeState_old);

        Integer insertint = overtimeStateMapper.updateById(overtimeState);

        if(insertint==0){
            return 0;
        }

        Integer otsState = 0;

        if(overtimeState.getOtsState() == 1 && overtimeState.getOtsPost()== 2){
            otsState = 1;
        }else if(overtimeState.getOtsState() == -1 && overtimeState.getOtsPost()== 2){
            otsState = -1;
        }else if(overtimeState.getOtsState() == 1 && overtimeState.getOtsPost()== 1){
            otsState = 2;
        }else if(overtimeState.getOtsState() == -1 && overtimeState.getOtsPost()== 1){
            otsState = -2;
        }


        OvertimeState overtimeState1 = queryOvertimeStateasc(overtimeState.getOtsOvertime());

        Overtime overtime = new Overtime();
        Overtime overtimeForm = new Overtime();
        overtimeForm.setOtState(otsState);
        overtimeForm.setOtUpdateUser(shiroUser.getUserId());
        overtimeForm.setOtUpdateDate(new Date());
        overtimeForm.setOtId(overtimeState.getOtsOvertime());
        if(StringUtils.isNotEmpty(overtimeState1)){
            overtimeForm.setOtNextAuditor(overtimeState1.getOtsAuditor());
        }
        if(overtimeState.getOtsPost() ==1){
            overtimeForm.setOtNextAuditor(0);
        }
        BeanUtils.copyProperties(overtimeForm,overtime);

        return overtimeMapper.updateById(overtime);
    }

    @Override
    public Integer update(OvertimeState overtimeStateForm) {
        OvertimeState overtimeState = new OvertimeState();
        BeanUtils.copyProperties(overtimeStateForm,overtimeState);
        return overtimeStateMapper.updateById(overtimeState);
    }

    @Override
    public Integer delete(Integer otsId) {
        Wrapper<OvertimeState> wrapper = new EntityWrapper<>();
        wrapper.eq("ots_id", otsId);
        return overtimeStateMapper.delete(wrapper);
    }

    @Override
    public List<Integer> queryOvertimeIdByauditor(Integer auditor){


        Wrapper<OvertimeState> wrapper = new EntityWrapper<>();
        wrapper.ne("ots_state",0);
        wrapper.eq("ots_auditor",auditor);
        wrapper.groupBy("ots_overtime");
        List<OvertimeState> overtimeStates = overtimeStateMapper.selectList( wrapper);

        List<Integer> overtims = new ArrayList<>();
        for (OvertimeState ov :overtimeStates){
            overtims.add(ov.getOtsOvertime());
        }

        return overtims;
    }

    @Override
    public OvertimeState queryOvertimeStateasc(Integer oId){

        OvertimeState overtimeState = new OvertimeState();

        Wrapper<OvertimeState> wrapper = new EntityWrapper<>();

        wrapper.eq("ots_overtime",oId);

        wrapper.eq("ots_state",0);

        wrapper.orderBy("ots_id");

        List<OvertimeState> overtimeStates = overtimeStateMapper.selectList( wrapper);

        if(overtimeStates.size()>0){
            overtimeState = overtimeStates.get(0);
        }

        return overtimeState;
    }

}
